package com.zgjkhis.modules.system.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.Drug;
import com.zgjkhis.modules.system.domain.DrugCirculation;
import com.zgjkhis.modules.system.req.DrugCirculationReq;
import com.zgjkhis.modules.system.req.DrugCirculationToReq;
import com.zgjkhis.modules.system.req.DrugReq;
import com.zgjkhis.modules.system.service.DrugCirculationService;
import com.zgjkhis.modules.system.service.DrugService;
import com.zgjkhis.modules.system.service.dto.DrugQueryCriteria;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @date 2018-11-23
 */
@Api(tags = "药品流转：药品流转管理")
@RestController
@RequestMapping("/api/circulation")
public class DrugCirculationController {

    private static final String ENTITY_NAME = "circulation";
    private final DrugCirculationService drugCirculationService;
    private final DrugService drugService;

    public DrugCirculationController(DrugCirculationService drugCirculationService, DrugService drugService) {
        this.drugCirculationService = drugCirculationService;
        this.drugService = drugService;
    }

    @Log("分页查询药品")
    @ApiOperation("分页查询药品")
    @GetMapping(value = "/getDrugs")
    public ResponseEntity<PageResult<Drug>> getDrugs(DrugReq drugReq, Pageable pageable) {
        DrugQueryCriteria criteria = new DrugQueryCriteria();
        criteria.setPharmacyId(drugReq.getPharmacyId());
        criteria.setDrugName(drugReq.getDrugName());
        criteria.setDrugType(7);
        return new ResponseEntity<>(drugService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

    @Log("查看单个药品流转")
    @GetMapping(value = "/detail")
    @ApiOperation("查看药品流转")
    public ResponseEntity<Object> getDetail(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        return new ResponseEntity<>(drugCirculationService.getById(id), HttpStatus.OK);
    }

    @Log("新增药品流转")
    @ApiOperation("新增药品流转")
    @PostMapping(value = "/add")
    public ResponseEntity<Object> create(@Validated @RequestBody DrugCirculation resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        DrugCirculation circulation = drugCirculationService.getOne(Wrappers.<DrugCirculation>query().lambda().eq(DrugCirculation::getFromDrugId, resources.getFromDrugId()).ne(DrugCirculation::getStatus, -1));
        if (!CharUtil.isEmpty(circulation)) {
            throw new ServiceException("该药品存在流转药品");
        }
        resources.setCreateUser(LoginUserUtil.getNickName());
        resources.setCreateUserId(SecurityUtils.getUserId().toString());
        return new ResponseEntity<>(drugCirculationService.save(resources), HttpStatus.OK);
    }

    @Log("修改药品流转")
    @ApiOperation("修改药品流转")
    @PostMapping(value = "/update")
    public ResponseEntity<Object> update(@Validated @RequestBody DrugCirculation resources) {
        drugCirculationService.saveOrUpdate(resources);
        return ResponseEntity.ok(true);
    }

    @Log("查询药品流转")
    @ApiOperation("查询药品流转")
    @GetMapping(value = "/getDrugCirculation")
    public ResponseEntity<Object> getDrugCirculation(DrugCirculationReq drugCirculationReq, Pageable pageable) {
        drugCirculationReq.setFromStoreId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(drugCirculationService.queryAll(drugCirculationReq, pageable), HttpStatus.OK);
    }

    @Log("查询药品流转去向")
    @ApiOperation("查询药品流转去向")
    @GetMapping(value = "/getDrugCirculationTo")
    public ResponseEntity<Object> getDrugCirculationTo(DrugCirculationToReq drugCirculationToReq, Pageable pageable) {
        drugCirculationToReq.setStoreId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(drugCirculationService.queryCirculationAll(drugCirculationToReq, pageable), HttpStatus.OK);
    }

}
